﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MyRaceGame.GameLogic.GameStates
{
    public class MainMenuState : RaceGameState
    {
        public MainMenuState(RaceGame raceGame)
            : base(raceGame)
        {
            InputController.Clicked += new InputControl.ClickEventHandler(InputController_Clicked);
            InputController.MouseMoved += new InputControl.MouseMoveEventHandler(InputController_MouseMoved);

            loadTextureFromMenuImages(ref BackTex, "back");

            loadTextureFromMenuImages(ref newTex, "new");
            loadTextureFromMenuImages(ref howTex, "how");
            loadTextureFromMenuImages(ref exitTex, "exit");
            loadTextureFromMenuImages(ref newTexS, "new_selected");
            loadTextureFromMenuImages(ref howTexS, "how_selected");
            loadTextureFromMenuImages(ref exitTexS, "exit_selected");

            addMenuItem("New Game", newTex, newTexS);
            addMenuItem("How to Play", howTex, howTexS);
            addMenuItem("Exit", exitTex, exitTexS);

            raceGame.IsMouseVisible = true;

            if (!raceGame.Graphics.IsFullScreen)
                raceGame.ToggleFullScreen();
        }

        void loadTextureFromMenuImages(ref Texture2D texture, string name)
        {
            if (texture == null)
                texture = raceGame.Content.Load<Texture2D>(@"MenuImages\" + name);
        }

        protected override void DrawingAdditional(GameTime gameTime)
        {
            raceGame.SpriteBatch.Begin();

            raceGame.SpriteBatch.Draw(BackTex, Vector2.Zero, Color.White);

            foreach (MenuItem menuItem in menuItems)
                if (menuItem.Selected)
                    raceGame.SpriteBatch.Draw(menuItem.SelectedTexture, menuItem.Position, Color.White);
                else
                    raceGame.SpriteBatch.Draw(menuItem.Texture, menuItem.Position, Color.White);

            raceGame.SpriteBatch.End();
        }

        void addMenuItem(string name, Texture2D tex, Texture2D selectedTex)
        {
            menuItems.Add(new MenuItem(name, tex, selectedTex, new Vector2(384, 40 + 95 * menuItems.Count + 10)));
        }

        void InputController_MouseMoved(Vector2 position)
        {
            foreach (MenuItem menuItem in menuItems)
                menuItem.Selected = menuItem.Bounds.Contains((int)position.X, (int)position.Y);
        }

        void InputController_Clicked(bool right, Vector2 position)
        {
            if (right)
                return;

            MenuItem clickedMenuItem = null;

            foreach (MenuItem menuItem in menuItems)
                if (menuItem.Bounds.Contains((int)position.X, (int)position.Y))
                    clickedMenuItem = menuItem;

            if (clickedMenuItem == null)
                return;

            switch (clickedMenuItem.Name)
            {
                case "New Game":
                    raceGame.IsMouseVisible = false;
                    InputController.Clicked -= InputController_Clicked;
                    InputController.MouseMoved -= InputController_MouseMoved;
                    GameState = new BeginningState(raceGame);
                    break;
                case "How to Play":
                    InputController.Clicked -= InputController_Clicked;
                    InputController.MouseMoved -= InputController_MouseMoved;
                    GameState = new HtPMenuState(raceGame);
                    break;
                case "Exit":
                    raceGame.Exit();
                    break;
            }
        }
        
        List<MenuItem> menuItems = new List<MenuItem>();
        static Texture2D newTex, howTex, exitTex;
        static Texture2D newTexS, howTexS, exitTexS;
        public static Texture2D BackTex;
    }
}
